package com.hos.check.ds;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class DBAttribute {

	private final Log log = LogFactory.getLog(DBAttribute.class);

	public void testConnection() throws Exception {
		
//		sysdict.getCodekind();
//		
//		testConnection(dbcpForm.getFdDriver(), dbcpForm.getFdUrl(), dbcpForm
//				.getFdUsername(), dbcpForm.getFdPassword());
	}

	public void testConnection(String driver, String url, String username,
			String password) throws Exception {
		Class.forName(driver).newInstance();
		Connection conn = DriverManager.getConnection(url, username, password);
		conn.close();
	}

	public List<Map> getSelectValues(String dbcpName, String sql)
			throws Exception {
		DataSet ds = new DataSet(dbcpName);
		try {
			return getSelectValues(ds, sql);
		} catch (Exception ex) {
			ex.printStackTrace();
			return null;
		} finally {
			ds.close();
		}
	}

	public List<Map> getSelectValues(DataSet ds, String sql) throws Exception {
		List<Map> list = new ArrayList<Map>();
		if (log.isDebugEnabled()) {
			log.debug("sql::" + sql);
		}
		ResultSet result = ds.executeQuery(sql);
		int count = result.getMetaData().getColumnCount();
		while (result.next()) {
			Map<String, String> map = new HashMap<String, String>();
			map.put("value", result.getString(1));
			map.put("text", result.getString(1));
			if (count == 2) {
				map.put("text", result.getString(2));
			}
			list.add(map);
		}
		return list;
	}

	public static void main(String[] args) throws Exception {
		Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
		Connection conn = DriverManager
				.getConnection(
						"jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS_LIST=(ADDRESS =(PROTOCOL =TCP)(HOST =192.0.1.85)(PORT = 1521)))(CONNECT_DATA =(SERVICE_NAME =dyyy)))",
						"interface", "interface");
		PreparedStatement prepstmt = conn
				.prepareStatement("select * from v_sick_visit_info ");
		// prepstmt.setString(1, "rep");
		ResultSet rs = prepstmt.executeQuery();
		if (rs.next()) {
			System.out.print(rs.getString(1));
		}
	}
}
